package cn.zyjblogs.server.qrcode.controller;

import cn.zyjblogs.server.qrcode.dto.QrCode;
import cn.zyjblogs.server.qrcode.service.QrCodeSerive;
import cn.zyjblogs.starter.common.entity.response.HttpCode;
import cn.zyjblogs.starter.common.entity.response.ResponseObject;
import cn.zyjblogs.starter.common.entity.response.ResponseResult;
import cn.zyjblogs.starter.common.exception.AuthRuntimeException;
import cn.zyjblogs.starter.web.apiversion.ApiVersion;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@ApiVersion(1)
@RequestMapping("/{v}/qrcode")
@RestController
@ResponseBody
@RequiredArgsConstructor
public class QrCodeController {
    private final QrCodeSerive qrCodeSerive;

    //获取登录二维码、放入Token
    @GetMapping("/create")
    @ApiVersion(1)
    public ResponseObject<QrCode> createCodeImg(@RequestParam("appid") String appid) {
        try {
            return ResponseResult.success(qrCodeSerive.createQrCode(appid));
        } catch (Exception e) {
            throw new AuthRuntimeException(HttpCode.BAD_REQUEST, e.getMessage());
        }
    }

    /**
     * 判断是否过期
     *
     * @param k
     * @param f
     * @return
     */
    @GetMapping("/poll")
    @ApiVersion(1)
    public ResponseObject<QrCode> poll(@RequestParam("k") String k, @RequestParam("f") String f) {
        try {
            return ResponseResult.success(qrCodeSerive.poll(k, f));
        } catch (Exception e) {
            throw new AuthRuntimeException(HttpCode.BAD_REQUEST, e.getMessage());
        }
    }

    /**
     * 扫码
     *
     * @param k qrsig
     * @param f clientId
     * @return
     */
    @GetMapping("/scan")
    @ApiVersion(1)
    public ResponseObject<Boolean> qrcodeScan(@RequestParam("k") String k, @RequestParam("f") String f) {
        try {
            return ResponseResult.success(qrCodeSerive.qrcodeScan(k, f));
        } catch (Exception e) {
            throw new AuthRuntimeException(HttpCode.BAD_REQUEST, e.getMessage());
        }
    }


    /**
     * 获取登录token
     *
     * @param k
     * @param f
     * @return
     */
    @GetMapping("/login")
    @ApiVersion(1)
    public ResponseObject<Boolean> getQrCodeLogin(@RequestParam("k") String k, @RequestParam("f") String f) {
        try {
            return ResponseResult.success(qrCodeSerive.getQrCodeLogin(k, f));
        } catch (Exception e) {
            throw new AuthRuntimeException(HttpCode.BAD_REQUEST, e.getMessage());
        }
    }
}
